import time,ntptime,network,socket,_thread,socket,sys
from machine import Pin,I2C,UART,PWM, SPI
from machine import Timer
import st7789py as st

#创建串口对象
uart1=UART(1, baudrate=9600, tx=4, rx=15)
uart2=UART(2, baudrate=19200, tx=17, rx=16)
# 构造一个硬件SPI对象        
spi=SPI(2, baudrate=30000000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=Pin(13), mosi=Pin(12), miso=Pin(22))
# 创建显示屏对象
tft = st.ST7789(spi, 320, 240, reset=Pin(14), dc=Pin(27), cs=Pin(26), backlight=Pin(25), rotation=0)
BLK = PWM(Pin(25))
BLK.freq(200)
BLK.duty(100)
    
#并将网络凭据（ssid和密码）存储在两个变量上。
SSID = "Sensen"
PASSWORD = "15367660"
#获取站点WiFi接口的实例并将其存储在变量上
station = network.WLAN(network.STA_IF)
clientID = "4892bd7fe005ecbbccf35929157ec7e7" # Client ID ，密钥，巴法云控制台获取
serverIP = 'bemfa.com'    #服务器地址
port = 8344         #服务器端口


k1=0
k2=0
k3=0
k4=0
k5=0
k6=0
k7=0
k8=0
U=0
I=0
P=0
W=0
r=0
client = None


#连接程序
def connect():
            try:
                #如已连接，则打印警告，完成执行
                if station.isconnected() == True:
                    print(station.ifconfig())
                    return
                #如未连接，则使用存储在前文所述的变量中的凭证来激活网络接口并执行实际连接。
                connect_wlan(SSID, PASSWORD)
                while station.isconnected() == False:
                    shijian()
                    time.sleep(1)
                print(station.ifconfig())
            except Exception as e:
                shijian()
                print("没有搜到Wifi",e)
                time.sleep(1)



    
da=[0,0,0,0,0]
def CRC0():
        global da
        uart1.write(bytes([0x01,0x03,0x00,0x48,0x00,0x08,0xC4,0x1A])) # 发送该HEX数据
        time.sleep(0.1)
        shuju = uart1.read()
        if shuju!= None :
            test_crc =0xFFFF
            test_crc1=0X0000
            poly=0xa001
            shuju1 =(shuju[36:])
            numl=len(shuju1)
            for num in range(numl):
                test_crc1=hex(shuju1[num])#  test_crc1 接收到的效验码
            shuju =(shuju[0:-2])
            numl=len(shuju)
            for num in range(numl):
                data=int(shuju[num])

                test_crc=(data&0xFF)^test_crc   #把第一个8位二进制数据（既通讯信息帧的第一个字节）与16位的CRC寄存器的低8位相异或，把结果放于CRC寄存器，高八位数据不变；
                for bit in range(8):
                    if(test_crc&0x1)!=0:
                        test_crc>>=1
                        test_crc^=poly
                    else:
                        test_crc>>=1
            test_crc=hex(test_crc)[:4]#   test_crc 计算到的效验码

            if  test_crc==test_crc1:
                    data = shuju.hex()
                    da[0] =int(int((data[6:14]),16)/1000)
                    da[1] =int(int((data[14:22]),16)/1000)
                    da[2] =int(int((data[22:30]),16)/1000)
                    da[3] =int(int((data[30:38]),16)/1000)
                    da[4] =int(int((data[38:46]),16))

        else:
            da=[0,0,0,0,0]
 
ta=[0,0,0,0,0]            
def CRC1():
        global ta
        uart1.write(bytes([0x02,0x03,0x00,0x48,0x00,0x08,0xC4,0x29])) # 发送该HEX数据
        time.sleep(0.1)
        shuju = uart1.read()
        if shuju != None:
            test_crc =0xFFFF
            test_crc1=0X0000
            poly=0xa001
            shuju1 =(shuju[36:])
            numl=len(shuju1)
            for num in range(numl):
                test_crc1=hex(shuju1[num])#  test_crc1 接收到的效验码
            shuju =(shuju[0:-2])   
            numl=len(shuju)
            for num in range(numl):
                data=int(shuju[num])
                test_crc=(data&0xFF)^test_crc   #把第一个8位二进制数据（既通讯信息帧的第一个字节）与16位的CRC寄存器的低8位相异或，把结果放于CRC寄存器，高八位数据不变；
                for bit in range(8):
                    if(test_crc&0x1)!=0:
                        test_crc>>=1
                        test_crc^=poly
                    else:
                        test_crc>>=1
            test_crc=hex(test_crc)[:4]    #   test_crc 计算到的效验码
            if  test_crc==test_crc1:
                    data = shuju.hex()
                    ta[0] =int(int((data[6:14]),16)/1000)
                    ta[1] =int(int((data[14:22]),16)/1000)
                    ta[2] =int(int((data[22:30]),16)/1000)
                    ta[3] =int(int((data[30:38]),16)/1000)
                    ta[4] =int(int((data[38:46]),16))
        else:
            ta=[0,0,0,0,0]
OUT = PWM(Pin(5))
OUT.freq(1000)
OUT.duty(0)
# P0 = 0     #用电功率
# P1 = 0     #并网功率
# P2 = 0    #功率差
A  = 100    #误差值
B  = 0.01   #并网电流递增值
C  = 200    #最大功率W
D  = 30     #最小功率W
i  = 1      #PWM
def ACIM0():
        time.sleep(5)
        while k1 == 1:
            CRC0()#用电功率
            CRC1()#并网功率
        OUT.duty(0)
        CRC1()#并网功率
        CRC0()#用电功率
        i  = 1 #PWM
        
            
def ACIM1():
        time.sleep(5)
        while da[2] > D*10 and k1 == 1:
            AC()
        OUT.duty(0)
        i  = 1 #PWM

       
def AC():
        global i
        time.sleep(0.001)
        P0 = da[2]
        P1 = ta[2]
        P2 = int(P0 - P1)
        B = abs(P2)
        if P2 > A and P1 < C*10:
            i +=(B/10000)
            if i > 1020:
              i = 1020
            OUT.duty(int(i))
  
        elif P2 < 0:
            i -=(B/1000)
            if i < 0:
              i = 0
            OUT.duty(int(i))
                  
def IM1253B():
                AC1=da[2:3]
                AC2=ta[2:3]
                AC3=da[1:2]
                AC4=ta[1:2]
                AC5=ta[3:4]
                AC1=int("".join(list(map(str,AC1))))
                AC1=AC1.to_bytes(2,0)
                AC1=[ab for ab in AC1]
                AC2=int("".join(list(map(str,AC2))))
                AC2=AC2.to_bytes(2,0)
                AC2=[ab for ab in AC2]
                AC3=int("".join(list(map(str,AC3))))
                AC3=AC3.to_bytes(2,0)
                AC3=[ab for ab in AC3]
                AC4=int("".join(list(map(str,AC4))))
                AC4=AC4.to_bytes(2,0)
                AC4=[ab for ab in AC4]
                AC5=int("".join(list(map(str,AC5))))
                AC5=AC5.to_bytes(2,0)
                AC5=[ab for ab in AC5]
                ac = [0XFF,0X08,0X0B,0X01]
                ac.extend(AC1)
                ac.extend(AC2)
                ac.extend(AC3)
                ac.extend(AC4)
                ac.extend(AC5)
                sum=0
                for i in range(0,len(ac)):
                    sum+=ac[i]
                sum=sum & 0xff
                ac.append(sum)
#                 DC = [0XFF,0X08,0X07,0X00,0X31,0X39,0X00,0X99,0X03,0X15]
#                 AC = [0XFF,0X08,0X07,0X01,0X31,0X39,0X00,0X99,0X03,0X15]
#                 print(" DC发送的数据",DC)
#                 print(" AC发送的数据",AC)
                
                uart2.write(bytes(ac))
                
                return ac

mppt=[0,0,0,0,0,0,0,0]            
def MPPT():
                global mppt
                # 1、创建一个UDP套接字
                udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                #非阻塞接收
#                 udp_socket.setblocking(True)

                # 2. 绑定本地的相关信息，如果不绑定，则系统会随机分配一个端口号
                local_addr = ('', 6060)
                # ip地址和端口号，ip一般不用写，表示本机的任何一个ip
                udp_socket.bind(local_addr)
                # 3. 等待接收对方发送的数据
                recv_data = udp_socket.recvfrom(2048)  # 1024表示本次接收的最大字节数
              
                # 4、解码接收到的数据
                recv_data=recv_data[0].decode()
         
                mppt[1] = float((recv_data.split(':'))[2])
                mppt[2] = float((recv_data.split(':'))[4])
                mppt[3] = float((recv_data.split(':'))[6])
                mppt[4] = float((recv_data.split(':'))[8])
                mppt[5] = float((recv_data.split(':'))[10])
                mppt[6] = float((recv_data.split(':'))[12])
                mppt[7] = float((recv_data.split(':'))[14])
                
                A=int(mppt[1]*100).to_bytes(2,0)
                A=[ab for ab in A]
                B=int(mppt[2]*100).to_bytes(2,0)
                B=[ab for ab in B]
                C=int(mppt[3]*100).to_bytes(2,0)
                C=[ab for ab in C]
                D=int(mppt[4]*100).to_bytes(2,0)
                D=[ab for ab in D]
                E=int(mppt[5]*100).to_bytes(2,0)
                E=[ab for ab in E]
      
                M = [0XFF,0X08,0X0B,0X02]
                M.extend(A)
                M.extend(B)
                M.extend(C)
                M.extend(D)
                M.extend(E)
                sum=0
                for i in range(0,len(M)):
                    sum+=M[i]
                sum=sum & 0xff
                M.append(sum)
#                 print(M)
                uart2.write(bytes(M))
                # 5. 关闭套接字
                udp_socket.close()
def AIZK_data():
            global k1,k2,k3,k4,k5,k6,k7,k8,U,I,W,P
            data = uart2.read()
            uart2.write(bytes([0XFF,0X06,0X01,0X55,0X5B]))
            time.sleep(0.1)
            data = uart2.read()
            if data != None and len(data) == 54:
                    k1 = int(bin(int(((data.hex())[5:8]),16))[-1:])
                    k2 = int(bin(int(((data.hex())[5:8]),16))[-2:-1])
                    k3 = int(bin(int(((data.hex())[5:8]),16))[-3:-2])
                    k4 = int(bin(int(((data.hex())[5:8]),16))[-4:-3])
                    k5 = int(bin(int(((data.hex())[5:8]),16))[-5:-4])
                    k6 = int(bin(int(((data.hex())[5:8]),16))[-6:-5])
                    k7 = int(bin(int(((data.hex())[5:8]),16))[-7:-6])
                    k8 = int(bin(int(((data.hex())[5:8]),16))[-8:-7])
                    U  = (int(((data.hex()[40:44])),16))
                    I  = (int(((data.hex()[44:48])),16))
                    W  = (int(((data.hex()[48:52])),16))
                    P  = (int(((data.hex()[52:56])),16))
            if client != None:
                if (k1 == 1):
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K1001/up&msg=on\r\n'
                    client.send(keeplive.encode("utf-8"))
                else :    
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K1001/up&msg=off\r\n'
                    client.send(keeplive.encode("utf-8"))            
                if (k2 == 1):
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K2001/up&msg=on\r\n'
                    client.send(keeplive.encode("utf-8"))
                else :   
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K2001/up&msg=off\r\n'
                    client.send(keeplive.encode("utf-8"))            
                if (k3 == 1):
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K3001/up&msg=on\r\n'
                    client.send(keeplive.encode("utf-8"))
                else :   
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K3001/up&msg=off\r\n'
                    client.send(keeplive.encode("utf-8"))            
                if (k4 == 1):
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K4001/up&msg=on\r\n'
                    client.send(keeplive.encode("utf-8"))
                else :   
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K4001/up&msg=off\r\n'
                    client.send(keeplive.encode("utf-8"))            
                if (k5 == 1):
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K5001/up&msg=on\r\n'
                    client.send(keeplive.encode("utf-8"))
                else :   
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K5001/up&msg=off\r\n'
                    client.send(keeplive.encode("utf-8"))
                if (k6 == 1):
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K6001/up&msg=on\r\n'
                    client.send(keeplive.encode("utf-8"))
                else :   
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K6001/up&msg=off\r\n'
                    client.send(keeplive.encode("utf-8"))                
                if (k7 == 1):
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K7001/up&msg=on\r\n'
                    client.send(keeplive.encode("utf-8"))
                else :   
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K7001/up&msg=off\r\n'
                    client.send(keeplive.encode("utf-8"))                
                if (k8 == 1):
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K8001/up&msg=on\r\n'
                    client.send(keeplive.encode("utf-8"))
                else :   
                    keeplive = 'cmd=2&uid='+clientID+'&topic=K8001/up&msg=off\r\n'
                    client.send(keeplive.encode("utf-8"))                
                

def AIZK():
            try:
                AIZK_data()
      
            except Exception as e:
                print("AIZK_data",e)
                
            try:
                IM1253B()
            except Exception as e:
                print("IM1253B",e)
                
#             try:
#                 MPPT()
#             except Exception as e:
#                 pass
#                 print("MPPT",e)


def xdc_TFT():
    tft.text(32, "%s.XuDianChi  "% (val), 0, 38,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "U:%s V     "% (mppt[1]), 0, 90,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "I:%s A     "% (mppt[2]), 0, 130,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "W:%s W     "% (int(mppt[1])*int(mppt[2])), 0, 170,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "           ", 0, 207,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "           ", 0, 246,st.color565(0, 0, 0), st.color565(255, 255, 255))            
    tft.text(32, "           ", 0, 285,st.color565(0, 0, 0), st.color565(255, 255, 255))            
def mppt_TFT():
    tft.text(32, "%s.MPPT       "% (val), 0, 38,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "U:%s V     "% (mppt[3]), 0, 90,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "I:%s A     "% (mppt[4]), 0, 130,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "W:%s W     "% (mppt[5]), 0, 170,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "P:%s kwh   "% (mppt[7]), 0, 210,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "T:%s C     "% (mppt[6]), 0, 250,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "           ", 0, 285,st.color565(0, 0, 0), st.color565(255, 255, 255))            


def ac_TFT():
    tft.text(32, "%s.BingWang   "% (val), 0, 38,st.color565(0, 0, 0), st.color565(255, 255, 255)) 
    tft.text(32, "U:%s V     "% (ta[0]/10), 0, 90,st.color565(0, 0, 0), st.color565(255, 255, 255)) 
    tft.text(32, "I:%s A     "% (ta[1]/10), 0, 129,st.color565(0, 0, 0), st.color565(255, 255, 255)) 
    tft.text(32, "W:%s W     "% (da[2]/10), 0, 168,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "W:%s W     "% (ta[2]/10), 0, 207,st.color565(0, 0, 0), st.color565(255, 255, 255))
    tft.text(32, "PF:%s COS  "% (ta[4]/10), 0, 246,st.color565(0, 0, 0), st.color565(255, 255, 255))            
    tft.text(32, "OUT:%s PWM "% (int(i)), 0, 285,st.color565(0, 0, 0), st.color565(255, 255, 255))            


#按键1外部中断函数
def key1_irq(key1):
    global val,t
    time.sleep_ms(1) #按键消抖
    if key1.value()==0:
        val -= 1
        if val <= 0:
           val=3
        print(val,"K1按键被按下")

#按键2外部中断函数
def key2_irq(key2):
    global val,t
    time.sleep_ms(1) #按键消抖
    if key2.value()==0:
        val += 1
        if val >= 4:
           val=1
        print(val,"K2按键被按下")
#定义按键控制对象
key1=Pin(32, Pin.IN, Pin.PULL_UP)
key2=Pin(33, Pin.IN, Pin.PULL_UP)
key1.irq(key1_irq,Pin.IRQ_FALLING)#配置key1外部中断，下降沿触发
key2.irq(key2_irq,Pin.IRQ_FALLING)#配置key2外部中断，下降沿触发
val = 3
  
def TFT():
                global val
                try:
                    if station.isconnected() == True:
                        tft.text(32, 'Wifi', 0, 0,st.color565(0, 0, 250), st.color565(255, 255, 255))
                    else:
                        tft.text(32, 'Wifi', 0, 0,st.color565(0, 255, 255), st.color565(255, 255, 255))
                    shijian()

                    if val ==1:
                        xdc_TFT()
                    elif  val ==2:
                        mppt_TFT()
                    elif  val ==3:
                        ac_TFT()
                except Exception as e:
                    print("TFT",e)



def shijian():
        '''
        显示时间
        '''
        t = time.localtime(time.time())
        year = t[0]
        month = t[1]
        day = t[2]
        hour = t[3]
        minute = t[4]
        second = t[5]
        ti = "{:0>2d}:{:0>2d}:{:0>2d}".format(hour,minute,second)
        tft.text(16, ti, 110, 0, st.color565(0, 0, 250), st.color565(255, 255, 255))
def sync_ntp():
         while True:   #时间校准
            try:
                print(' 正在校准时间')
                """通过网络校准时间"""
                ntptime.NTP_DELTA = 3155644800  # 可选 UTC+8偏移时间（秒），不设置就是UTC0
                ntptime.host = 'ntp1.aliyun.com'  # 可选，ntp服务器，默认是"pool.ntp.org" 这里使用阿里服务器
                ntptime.settime()  # 修改设备时间,到这就已经设置好了
                print(' 时间以校准')
                break
            except Exception as e:
                print("sync_ntp",e)
                time.sleep(5)



def Bfy():
    try:
        global client,k1,k2,k3,k4,k5,k6,k7,k8,r
        data1 = U
        data2 = I
        data3 = W
        data4 = (int("".join(list(map(str,da[2:3])))))/10
        data5 = (int("".join(list(map(str,ta[2:3])))))/10
        data6 = (int("".join(list(map(str,ta[0:1])))))/10
        data7 = 88
        data8 = 88
        if station.isconnected() == False:
            connect()
            client = None
            return
        elif client == None:
            addr_info = socket.getaddrinfo(serverIP, port)
            addr = addr_info[0][-1]
            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   # 创建TCP的套接字,也可以不给定参数。默认为TCP通讯方式
            client.connect(addr)                                 # 设置要连接的服务器端的IP和端口,并连接
            client.send(('cmd=1&uid='+clientID+'&topic=''Auto,K1001,K2001,K3001,K4001,K5001,K6001,K7001,K8001,''\r\n').encode("utf-8"))
                
                
                
        data = client.recv(256)       # 从服务器端套接字中读取1024字节数据
        keeplive = 'cmd=2&uid='+clientID+'&topic=Auto/up&msg=Auto#''%d''#''%d''#''%d''#''%d''#''%d''#''%d''#''%d''#''%d''\r\n'%(data1,data2,data3,data4,data5,data6,data7,data8)
        client.send(keeplive.encode("utf-8"))
        if(len(data) > 60):
                data=data.decode('utf-8')
                if data.find('topic=K1001&msg=on')>0:
                    M=[0XFF,0X0A,0X02,0X00,0Xff,0x10]
                    uart2.write(bytes(M))
                    k1=1
                    print('k1打开')
                elif data.find('topic=K1001&msg=off')>0:
                    M=[0XFF,0X0A,0X02,0X00,0X00,0x11]
                    uart2.write(bytes(M))                            
                    k1=0
                    print('k1关闭')
                elif data.find('topic=K2001&msg=on')>0:
                    M=[0XFF,0X0A,0X02,0X01,0Xff,0x11]
                    uart2.write(bytes(M))
                    k2=1
                    print('k2打开')
                elif data.find('topic=K2001&msg=off')>0:
                    M=[0XFF,0X0A,0X02,0X01,0X00,0x12]
                    uart2.write(bytes(M))
                    k2=0
                    print('k2关闭')
                elif data.find('topic=K3001&msg=on')>0:
                    M=[0XFF,0X0A,0X02,0X02,0Xff,0x12]
                    uart2.write(bytes(M))
                    k3=1
                    print('k3打开')
                elif data.find('topic=K3001&msg=off')>0:
                    M=[0XFF,0X0A,0X02,0X02,0X00,0x13]
                    uart2.write(bytes(M))
                    k3=0
                    print('k3关闭')
                elif data.find('topic=K4001&msg=on')>0:
                    M=[0XFF,0X0A,0X02,0X03,0Xff,0x13]
                    uart2.write(bytes(M))
                    k4=1
                    print('k4打开')
                elif data.find('topic=K4001&msg=off')>0:
                    M=[0XFF,0X0A,0X02,0X03,0X00,0x14]
                    uart2.write(bytes(M))
                    k4=0
                    print('k4关闭')
                elif data.find('topic=K5001&msg=on')>0:
                    M=[0XFF,0X0A,0X02,0X04,0Xff,0x14]
                    uart2.write(bytes(M))
                    k5=1
                    print('k5打开')
                elif data.find('topic=K5001&msg=off')>0:
                    M=[0XFF,0X0A,0X02,0X04,0X00,0x15]
                    uart2.write(bytes(M))                            
                    k5=0
                    print('k5关闭')
                elif data.find('topic=K6001&msg=on')>0:
                    M=[0XFF,0X0A,0X02,0X05,0Xff,0x15]
                    uart2.write(bytes(M))
                    k6=1
                    print('k2打开')
                elif data.find('topic=K6001&msg=off')>0:
                    M=[0XFF,0X0A,0X02,0X05,0X00,0x16]
                    uart2.write(bytes(M))
                    k6=0
                    print('k2关闭')
                elif data.find('topic=K7001&msg=on')>0:
                    M=[0XFF,0X0A,0X02,0X06,0Xff,0x16]
                    uart2.write(bytes(M))
                    k7=1
                    print('k7打开')
                elif data.find('topic=K7001&msg=off')>0:
                    M=[0XFF,0X0A,0X02,0X06,0X00,0x17]
                    uart2.write(bytes(M))
                    k7=0
                    print('k7关闭')
                elif data.find('topic=K8001&msg=on')>0:
                    M=[0XFF,0X0A,0X02,0X07,0Xff,0x17]
                    uart2.write(bytes(M))
                    k8=1
                    print('k8打开')
                elif data.find('topic=K8001&msg=off')>0:
                    M=[0XFF,0X0A,0X02,0X07,0X00,0X18]
                    uart2.write(bytes(M))
                    k8=0
                    print('k8关闭')
                elif data.find('topic=K8001&msg=update')>0:
                    machine.reset()    
                AIZK_data()
                keeplive = 'cmd=2&uid='+clientID+'&topic=Auto/up&msg=K#''%d''#''%d''#''%d''#''%d''#''%d''#''%d''#''%d''#''%d''#\r\n'%(k1,k2,k3,k4,k5,k6,k7,k8)
                client.send(keeplive.encode("utf-8"))    

    except Exception as e:
        r=r+1
        client = None
        print("Bfy",e)
        if r == 3:
            machine.reset()
            
def main0():   
        while True:
            gc.collect()
            TFT()
            Bfy()
            AIZK()
                    
def main1():
        while True:
            MPPT()
           
def main2():
        while True:
             ACIM0()
           
             
def main3():
        while True:
             ACIM1()

def main():
        gc.collect()
        # _thread.start_new_thread(main0, ())
        # _thread.start_new_thread(main1, ())
        # _thread.start_new_thread(main2, ())
        # _thread.start_new_thread(main3, ())
        main0()




if __name__ == "__main__":
         main()
        


